package org.hackystat.ui.telemetryviewer.gwt.client;

import java.util.ArrayList;
import java.util.Iterator;

import org.hackystat.ui.telemetryviewer.gwt.client.ChartWidget.ChartHandler;

/**
 * The objects that will handles all widget-browser history associations.
 *
 * @author Pavel Senin.
 *
 */
public class ChartListHolder implements Iterator {

  /* define the list holders first */
  private ArrayList charts;
  // private int selectedChart;
  private int iteratorIdx;

  /**
   * Instantiates the object.
   *
   */
  public ChartListHolder() {
    this.charts = new ArrayList();
    // this.selectedChart = -1;
    this.iteratorIdx = 0;
  }

  /**
   * Adds a chart into the collection.
   *
   * @param chartHandler the chart handler.
   */
  public void addChart(final ChartHandler chartHandler) {
    charts.add(chartHandler);
  }

  /**
   * Finds the specified by the name chart instance.
   *
   * @param chartName the chart name.
   * @return the chart Handler.
   */
  public ChartHandler find(String chartName) {
    for (int i = 0; i < charts.size(); ++i) {
      ChartHandler info = (ChartHandler) charts.get(i);
      if (info.getName().equals(chartName)) {
        return info;
      }
    }
    return null;
  }

  /**
   * Reports the index of the chart.
   *
   * @param chartName the chart name.
   * @return the index within the collection.
   */
  public int findIdx(String chartName) {
    for (int i = 0; i < charts.size(); ++i) {
      ChartHandler info = (ChartHandler) charts.get(i);
      if (info.getName().equals(chartName)) {
        return i;
      }
    }
    return 0;
  }

  /** {@inheritDoc} */
  public boolean hasNext() {
    return this.iteratorIdx < this.charts.size();
  }

  /** {@inheritDoc} */
  public Object next() {
    this.iteratorIdx = this.iteratorIdx + 1;
    return this.charts.get(this.iteratorIdx - 1);
  }

  /** {@inheritDoc} */
  public void remove() {
    this.charts.remove(this.iteratorIdx);
  }

  /**
   * Reprts an iterator over the charts.
   *
   * @return the iterator.
   */
  public Iterator iterator() {
    this.iteratorIdx = 0;
    return this;
  }

}
